package com.woniu.controller;

import com.woniu.aspect.OpreLog;
import com.woniu.constant.Constant;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.entity.dto.CarSearchDTO;
import com.woniu.entity.vo.CarListVO;
import com.woniu.pojo.Car;
import com.woniu.service.ICarService;
import com.woniu.service.ILogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 车辆控制器
 * @author tarnett
 */
@RestController
@RequestMapping("/car")
@Slf4j
@Api(tags = "车辆管理")
@CrossOrigin
public class CarController {

    @Autowired
    private ICarService carService;
    @Autowired
    private ILogService logService;

    @ApiOperation("搜索分页")
    @PostMapping("/search")
    public Result<PageResult<CarListVO>> listSearch(@RequestBody CarSearchDTO carSearchDTO){
        // 1. 对数据进行规则校验 TODO
        // 2. 调用业务逻辑层处理数据
        PageResult<CarListVO> pageResult = carService.listSearch(carSearchDTO);
        // 3. 接收数据，封装响应结果
        // 4. 返回数据
        return Result.ok(pageResult);
    }

    @OpreLog(title = "车辆模块", businessType = Constant.BUSINESS_TYPE_SAVE)
    @ApiOperation("保存车辆")
    @PostMapping
    public Result save(@RequestBody Car car, HttpServletRequest request){
        Result result = carService.save(car);
        return result;
    }

    @OpreLog(title = "车辆模块", businessType = Constant.BUSINESS_TYPE_DELETE)
    @ApiOperation("批量删除")
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable Long[] ids){
        Integer row = carService.deleteByIds(ids);
        return Result.commonByRow(row);
    }

    @OpreLog(title = "车辆模块", businessType = Constant.BUSINESS_TYPE_UPDATE)
    @ApiOperation("修改车辆")
    @PutMapping
    public Result update(@RequestBody Car car){
        Result result = carService.updateById(car);
        return result;
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    public Result<Car> getById(@PathVariable Long id){
        Car car = carService.getById(id);
        return Result.ok(car);
    }

    @ApiOperation("标题是否存在")
    @GetMapping("/check/title/{title}")
    public Result<Car> checkTitleExist(@PathVariable String title){
        Car car = carService.checkTitleExist(title);
        return Result.ok(car);
    }

    @OpreLog(title = "车辆模块", businessType = Constant.BUSINESS_TYPE_DELETE)
    @ApiOperation("修改显示状态")
    @PutMapping("/show/{id}/{val}")
    public Result updateShowStatus(@PathVariable Long id, @PathVariable String val){
        Integer row = carService.updateShowStatus(id, val);
        return Result.commonByRow(row);
    }
}
